import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']

# 加载数据
data = pd.read_csv("C:\\Users\\董柳蕙\\Downloads\\heart.csv")

# 数值型变量列表
numerical_columns = [col for col in data.columns if col not in ['sex', 'cp', 'fbs', 'restecg', 'exang', 'slope',  'target']]  # 替换为您的分类变量

# 绘制数值变量的箱线图进行异常值检测
plt.figure(figsize=(16, 10))
for i, col in enumerate(numerical_columns, 1):
    plt.subplot(3, 3, i)  # 根据变量数量调整网格布局
    sns.boxplot(x=data[col], color='skyblue')
    plt.title(f'{col} - 箱线图', fontsize=12)
    plt.xlabel('值', fontsize=10)
    plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()
